<?php

/**
 * @BelongProject: flami_mangers
 * @Author: Zwc
 * @CreateTime: 2025-11-09 22:13:48
 * @Description: TODO
 */

namespace app\admin\controller;

use app\admin\service\TaskService;
use cmf\controller\AdminBaseController;

class TaskController extends AdminBaseController
{

    public function index()
    {
        $params = $this->request->get();

        $countryData = $this->recommendLanguageData;
        $this->assign("countryData", $countryData);
        $this->assign('defaultLangId', $this->defaultLanguage['id']);

        if (empty($params['langId'])) {
            $params['langId'] = $this->defaultLanguage['id'];
            $this->assign('langId', $this->defaultLanguage['id']);
        }else {
            $this->assign('langId', $params['langId']);
        }
        $list = TaskService::getData($params);
        $list->appends($params);
        $page = $list->render();
        $this->assign('page', $page);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $service = new TaskService();
            $resData = $service->add($params);
            if ($resData['code'] == 0) {
                $this->success($resData['msg']);
            }else {
                $this->error($resData['msg']);
            }
        }else {
            $countryData = $this->recommendLanguageData;
            $this->assign("countryData", $countryData);
            $this->assign('taskTypeData', TaskService::getTypeData());
            return $this->fetch();
        }
    }

    public function edit()
    {
        $service = new TaskService();
        if (request()->isPost()) {
            $params = $this->request->post();
            $resData = $service->modify($params);
            if ($resData['code'] == 0) {
                $this->success($resData['msg']);
            }else {
                $this->error($resData['msg']);
            }
        }else {
            $params = $this->request->get();
            $countryData = $this->recommendLanguageData;
            $this->assign("countryData", $countryData);
            $this->assign('data', TaskService::detail($params['id']) );
            $this->assign('taskTypeData', TaskService::getTypeData());
            return $this->fetch();
        }
    }

    public function del() {
        $params = $this->request->get();
        $service = new TaskService();
        $res = $service->del($params);
        if ($res['code'] == 0) {
            $this->success($res['msg']);
        }else {
            $this->error($res['msg']);
        }
    }


}